@extends('cms/system/admin/template/theme_default/common')
@section('page-body')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form layui-form-pane" id="office-project-form" lay-filter="office-project-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属应用</label>
                        @if(isset($app) && $app)
                            <div class="layui-input-block">
                                <input id="app-id" class="layui-input" type="hidden" name="app_id" value="{{$app['id'] ?? 0}}" />
                                <input class="layui-input" type="text" value="{{$app['name'] ?? ''}}" placeholder="请选择所属应用" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @else
                            <div class="layui-input-block">
                                <input id="app-id" class="layui-input" type="hidden" name="app_id" value="{{$data['app_id'] ?? 0}}" />
                                <input id="app-select" class="layui-input" type="text" value="{{$data['app']['name'] ?? ''}}" placeholder="请选择所属应用" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @endif
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="name" value="{{$data['name'] ?? ''}}" placeholder="请输入名称" autocomplete="off" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">服务企业</label>
                        @if(isset($data) && $data['enterprise_id'])
                            <div class="layui-input-block">
                                <input id="enterprise-id" class="layui-input" type="hidden" name="enterprise_id" value="{{$data['enterprise_id'] ?? 0}}" />
                                <input id="enterprise-select" class="layui-input" type="text" value="{{$data['enterprise']['name'] ?? ''}}" placeholder="请选择服务企业" autocomplete="off" readonly />
                            </div>
                        @else
                            <div class="layui-input-block">
                                <input id="enterprise-id" class="layui-input" type="hidden" name="enterprise_id" value="0" />
                                <input id="enterprise-select" class="layui-input" type="text" value="" placeholder="请选择服务企业" autocomplete="off" readonly />
                            </div>
                        @endif
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">执行时间</label>
                        <div class="layui-input-block">
                            @php
                                $date_range = '';
                                if (isset($data) && $data['start_at'] && $data['finish_at']) {
                                    $date_range = date('Y-m-d', strtotime($data['start_at'])).' 至 '.date('Y-m-d', strtotime($data['finish_at']));
                                }
                            @endphp
                            <input id="start_at" class="layui-input" type="hidden" name="start_at" value="{{$data['start_at'] ?? ''}}" />
                            <input id="finish_at" class="layui-input" type="hidden" name="finish_at" value="{{$data['finish_at'] ?? ''}}" />
                            <input id="date-range" class="layui-input" type="text" value="{{$date_range}}" placeholder="年-月-日 至 年-月-日" autocomplete="off" readonly />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">合同金额</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="amount" value="{{$data['amount'] ?? ''}}" placeholder="合同金额，单位元" autocomplete="off" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">执行阶段</label>
                        <div class="layui-input-block">
                            <select id="phase" name="phase" lay-filter="phase">
                                <option value="0" {{isset($data) && $data['phase'] == 0 ? 'selected' : ''}}>商机</option>
                                <option value="1" {{isset($data) && $data['phase'] == 1 ? 'selected' : ''}}>立项</option>
                                <option value="2" {{isset($data) && $data['phase'] == 2 ? 'selected' : ''}}>执行</option>
                                <option value="3" {{isset($data) && $data['phase'] == 3 ? 'selected' : ''}}>收款</option>
                                <option value="4" {{isset($data) && $data['phase'] == 4 ? 'selected' : ''}}>完结</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注说明</label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" name="excerpt" placeholder="请输入内容">{{$data['excerpt'] ?? ''}}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <input class="layui-btn" type="button" value="添加" id="submit-create" lay-submit lay-filter="submit-create" />
                            <input class="layui-btn" type="button" value="更新" id="submit-update" lay-submit lay-filter="submit-update" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        layui.config({
            base: '/layui-admin/'
        }).extend({
            index: 'lib/index'
        }).use(['index', 'system'], function () {
            // region 渲染时间控件 +++++
            layui.laydate.render({
                elem: '#date-range',
                type: 'date',
                range: '至',
                done: function (value, start, finish) {
                    layui.jquery('#start_at').val(start.year + '-' + start.month + '-' + start.date);
                    layui.jquery('#finish_at').val(finish.year + '-' + finish.month + '-' + finish.date);
                }
            });
            // endregion

            // 选择应用
            layui.$('#app-select').on('click', function () {
                // region 处理已选中 +++++
                var selectValueElement = layui.$('#app-id');
                var selectValue = selectValueElement.val();
                var selectTextElement = layui.$('#app-select');
                var selectText = '';
                // endregion
                var selectLayer = layui.system.iframeLayer({
                    sourceWindow: window,
                    targetName: 'top',
                    title: '选择',
                    content: layui.setter.mvcLink + '/office/admin_app/select?app_id={{$app['id'] ?? 0}}&select_multi=0&select_value=' + selectValue,
                    btn: ['确定', '取消'],
                    yes: function (layerIndex, layerWindow) {
                        // 取得iframe窗口
                        var iframe = layerWindow.find('iframe')[0];
                        var selectResult = iframe.contentWindow.selectConfirm();
                        if (selectResult.data.length != 1) {
                            layui.layer.msg('请正确选择');
                            return false;
                        }
                        selectText = selectResult.data[0].name;
                        // 清空已有值，避免再次选择重复问题
                        selectValue = selectResult.data[0].id;
                        // 设置值
                        selectValueElement.val(selectValue);
                        selectTextElement.val(selectText);
                    }
                });
            });

            // 选择企业
            layui.$('#enterprise-select').on('click', function () {
                // region 处理已选中 +++++
                var selectValueElement = layui.$('#enterprise-id');
                var selectValue = selectValueElement.val();
                var selectTextElement = layui.$('#enterprise-select');
                var selectText = '';
                // endregion
                var selectLayer = layui.system.iframeLayer({
                    sourceWindow: window,
                    targetName: 'top',
                    title: '选择',
                    content: layui.setter.mvcLink + '/office/admin_enterprise/select?app_id={{$app['id'] ?? 0}}&select_multi=0&select_value=' + selectValue,
                    btn: ['确定', '取消'],
                    yes: function (layerIndex, layerWindow) {
                        // 取得iframe窗口
                        var iframe = layerWindow.find('iframe')[0];
                        var selectResult = iframe.contentWindow.selectConfirm();
                        if (selectResult.data.length != 1) {
                            layui.layer.msg('请正确选择配置');
                            return false;
                        }
                        selectText = selectResult.data[0].name;
                        // 清空已有值，避免再次选择重复问题
                        selectValue = selectResult.data[0].id;
                        // 设置值
                        selectValueElement.val(selectValue);
                        selectTextElement.val(selectText);
                    }
                });
            });

            // 创建
            layui.form.on('submit(submit-create)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/office/admin_project/create?app_id={{$app['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        if (result.code != 1) {
                            layui.layer.alert(result.msg);
                            return false;
                        }
                        // 刷新列表
                        window.SOURCE_WINDOW.layui.table.reload('office-project-list');
                    }
                });
            });
            // 更新
            layui.form.on('submit(submit-update)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/office/admin_project/update?app_id={{$app['id'] ?? 0}}&id={{$data['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        if (result.code != 1) {
                            layui.layer.alert(result.msg);
                            return false;
                        }
                        // 刷新列表
                        window.SOURCE_WINDOW.layui.table.reload('office-project-list');
                    }
                });
            });
        });
    </script>
@endsection